package stoyanov.calculator;


import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;

public class DropMenu {
	
	public static MenuItem newMenuField(Menu parent, String name) {
		MenuItem temp = new MenuItem (parent, SWT.CASCADE);
		temp.setText(name);
			
		return temp;
	}
	
	public static MenuItem newSubMenu(Menu submenu, String name) {
		MenuItem item = new MenuItem(submenu, SWT.PUSH);
		item.setText (name);
		
		return item;
	}

	public void createDropMenu(final Shell shell, final Display display) {
		Menu bar = new Menu(shell, SWT.BAR);
		shell.setMenuBar(bar);

		// File menu.
		
		MenuItem fileItem = newMenuField(bar, "&Calculator");
		Menu fSubmenu = new Menu(shell, SWT.DROP_DOWN);
		fileItem.setMenu(fSubmenu);
		
		MenuItem copyItem = newSubMenu(fSubmenu,"Copy");
		copyItem.setAccelerator(SWT.CTRL + 'C');
		
		MenuItem pasteItem = newSubMenu(fSubmenu,"Paste");
		pasteItem.setAccelerator(SWT.CTRL + 'V');
		
		MenuItem undoItem = newSubMenu(fSubmenu,"Undo");
		undoItem.setAccelerator(SWT.CTRL + 'V');

		MenuItem redoItem = newSubMenu(fSubmenu,"Redo");
		redoItem.setAccelerator(SWT.CTRL + 'V');

		MenuItem exitItem = newSubMenu(fSubmenu,"Quit");
		exitItem.setAccelerator(SWT.CTRL + 'E');
		exitItem.addListener(SWT.Selection, new Listener () {
			public void handleEvent (Event e) {
				System.exit(0);
			}
		});
		
		// Help menu.
		
		MenuItem helpItem = newMenuField(bar, "&Help");
		Menu helpSubmenu = new Menu(shell, SWT.DROP_DOWN);
		helpItem.setMenu(helpSubmenu);
		
		MenuItem helpSubItem = newSubMenu(helpSubmenu,"Help");
		helpSubItem.setAccelerator(SWT.CTRL + 'H');
		
		MenuItem aboutItem = newSubMenu(helpSubmenu,"About");
		aboutItem.setAccelerator(SWT.CTRL + 'A');

	}
}
